# coding=utf8
# 全世界最帅的帅哥
from flask import g, request
from .tokens_pyjwt import verify_tokens


def jwt_request_auth():
    """
    从request中获取并验证token
    :return: 验证成功之后，返回用户ID 保存到全局变量g
    """

    g.user_id = None
    # 从请求头中获取token
    token = request.headers.get("token")
    if token is not None:
        # 验证token
        result = verify_tokens(token)
        # 验证成功
        if "id" in result:
            g.user_id = result["id"]
